using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using SJVars;
using SJZip;
using Spacejock;
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using yW5Lib.My;

namespace yW5Lib
{
	public class clsProjectIO
	{
		public delegate void ProgressEventHandler(string ProgressString);

		[CompilerGenerated]
		private ProgressEventHandler ProgressEvent;

		private int ProjectLockFileNum;

		public event ProgressEventHandler Progress
		{
			[CompilerGenerated]
			add
			{
				ProgressEventHandler progressEventHandler = ProgressEvent;
				ProgressEventHandler progressEventHandler2;
				do
				{
					progressEventHandler2 = progressEventHandler;
					ProgressEventHandler value2 = (ProgressEventHandler)Delegate.Combine(progressEventHandler2, value);
					progressEventHandler = Interlocked.CompareExchange(ref ProgressEvent, value2, progressEventHandler2);
				}
				while ((object)progressEventHandler != progressEventHandler2);
			}
			[CompilerGenerated]
			remove
			{
				ProgressEventHandler progressEventHandler = ProgressEvent;
				ProgressEventHandler progressEventHandler2;
				do
				{
					progressEventHandler2 = progressEventHandler;
					ProgressEventHandler value2 = (ProgressEventHandler)Delegate.Remove(progressEventHandler2, value);
					progressEventHandler = Interlocked.CompareExchange(ref ProgressEvent, value2, progressEventHandler2);
				}
				while ((object)progressEventHandler != progressEventHandler2);
			}
		}

		public bool FolderExists(string Foldername)
		{
			return clsShared.cFile.FolderExists(Foldername);
		}

		public bool ReadTextAuto(string Filename, ref string Content, bool UseSysEncoding)
		{
			return clsShared.cFile.ReadTextAuto(Filename, ref Content, UseSysEncoding: true);
		}

		public bool ReadUnicodeForced(string Filename, ref string TextString)
		{
			return clsShared.cFile.ReadUnicodeForced(Filename, ref TextString);
		}

		public bool PasswordOk(string Password)
		{
			frmPasswordEntry frmPasswordEntry = new frmPasswordEntry(Password);
			frmPasswordEntry.ShowDialog();
			return frmPasswordEntry.PasswordOk;
		}

		public void MsgBox(string Message)
		{
			Interaction.MsgBox(Message);
		}

		public void MsgBoxWarning(string Message, string Title)
		{
			Interaction.MsgBox(Message, MsgBoxStyle.Exclamation, Title);
		}

		public bool ProjectIsLocked(string ProjectFilename)
		{
			bool result = default(bool);
			try
			{
				result = false;
				if (!clsShared.cFile.FileExists(LockFile(ProjectFilename)))
				{
					return result;
				}
				if (clsShared.cFile.EraseFile(LockFile(ProjectFilename)))
				{
					return result;
				}
				result = true;
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
		public bool LockProject(string ProjectFilename)
		{
			if (ProjectIsLocked(ProjectFilename))
			{
				return false;
			}
			bool result = default(bool);
			try
			{
				clsShared.cFile.WriteTextFile(LockFile(ProjectFilename), "Project locked " + Conversions.ToString(DateAndTime.Now));
				ProjectLockFileNum = FileSystem.FreeFile();
				FileSystem.FileOpen(ProjectLockFileNum, LockFile(ProjectFilename), OpenMode.Input, OpenAccess.Read);
				result = true;
				clsShared.cLog.AddAction("Locked project " + ProjectFilename);
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
		public bool UnlockProject(string ProjectFilename)
		{
			bool result = false;
			try
			{
				if (ProjectLockFileNum > 0)
				{
					FileSystem.FileClose(ProjectLockFileNum);
					ProjectLockFileNum = 0;
				}
				if (!clsShared.cFile.EraseFile(LockFile(ProjectFilename)))
				{
					return result;
				}
				result = true;
				clsShared.cLog.AddAction("Unlocked project " + ProjectFilename);
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		private string LockFile(string ProjectFilename)
		{
			if (Operators.CompareString(ProjectFilename, "", TextCompare: false) == 0)
			{
				return Path.Combine(MyProject.Application.Info.DirectoryPath, "yWriter5_Lock.lock");
			}
			return ProjectFilename + ".lock";
		}

		public bool BackupProject(clsProject cProject, bool WithSceneContent, string Destfolder, string DestFile)
		{
			//Discarded unreachable code: IL_0270, IL_0287, IL_0292, IL_02a2, IL_0358, IL_035a, IL_036e, IL_0390
			int num = default(int);
			bool result = false;
			int num3 = default(int);
			try
			{
				ProjectData.ClearProjectError();
				num = 2;
				int num2 = 2;
				ProgressEvent?.Invoke("Creating project backup");
				num2 = 3;
				clsShared.cFile.MkMissingDir(Destfolder);
				num2 = 4;
				string text = Path.Combine(Destfolder, DestFile);
				num2 = 5;
				clsZip clsZip = new clsZip();
				num2 = 6;
				clsZip.Setup();
				num2 = 7;
				clsZip.ZipFilename = text;
				num2 = 8;
				clsZip.AddSingleFile(clsShared.cSettings.CurrentSettingsFile);
				num2 = 9;
				clsShared.cLog.WriteActionsToLog();
				num2 = 10;
				if (clsShared.cFile.FileSize(clsShared.cLog.CurrentLogFilename()) < 5000000)
				{
					num2 = 11;
					clsZip.AddSingleFile(clsShared.cLog.CurrentLogFilename());
				}
				num2 = 12;
				ProgressEvent?.Invoke("Packing scenes into backup");
				num2 = 13;
				bool flag = default(bool);
				if (clsZip.MakeZipFile() == 0)
				{
					num2 = 14;
					clsUpdate clsUpdate = new clsUpdate();
					num2 = 15;
					Collection FileCol = new Collection();
					num2 = 16;
					FileCol.Add(cProject.FileWithPath);
					num2 = 17;
					if (!cProject.V6File)
					{
						num2 = 18;
						if (WithSceneContent)
						{
							num2 = 19;
							IEnumerator enumerator = cProject.Scenes.GetEnumerator();
							while (enumerator.MoveNext())
							{
								clsScene clsScene = (clsScene)enumerator.Current;
								num2 = 20;
								string item = Path.Combine(modSubs.RTFPathV5(cProject.ProjectPath()), clsScene.RTFFilename);
								num2 = 21;
								FileCol.Add(item);
								num2 = 22;
							}
							num2 = 23;
							if (enumerator is IDisposable)
							{
								(enumerator as IDisposable).Dispose();
							}
						}
					}
					num2 = 24;
					flag = clsUpdate.AddOrUpdateMultipleFiles(text, cProject.ProjectPath(), ref FileCol);
				}
				num2 = 25;
				if (flag)
				{
					num2 = 26;
					clsShared.cLog.AddAction("Backup to " + text + " complete");
					num2 = 27;
					ProgressEvent?.Invoke("Backup to " + clsShared.cFile.FileOnly(text) + " complete");
					num2 = 28;
					result = true;
					num2 = 29;
					if (cProject.V6File)
					{
						num2 = 30;
						cProject.LastFullBackup = DateAndTime.Now;
						num2 = 31;
						cProject.LastFullBackupFilename = text;
					}
					else
					{
						num2 = 33;
						if (WithSceneContent)
						{
							num2 = 34;
							cProject.LastFullBackup = DateAndTime.Now;
							num2 = 35;
							cProject.LastFullBackupFilename = text;
						}
					}
				}
				else
				{
					num2 = 37;
					clsShared.cLog.AddAction("Backup to " + text + " failed");
					num2 = 38;
					result = false;
				}
			}
			catch (Exception obj) when ((obj is Exception && num != 0) & (num3 == 0))
			{
				ProjectData.SetProjectError((Exception)obj);
				/*Error near IL_038e: Could not find block for branch target IL_035a*/;
			}
			if (num3 != 0)
			{
				ProjectData.ClearProjectError();
			}
			return result;
		}

		public void ZipCurrentProject(string ProjectFilewithpath, string BackupFolder)
		{
			try
			{
				clsShared.cFile.MkMissingDir(BackupFolder);
				clsZip clsZip = new clsZip();
				string text = Path.Combine(BackupFolder, clsShared.cFile.FileOnly(ProjectFilewithpath) + "_Latest.zip");
				if (clsShared.cFile.FileExists(text))
				{
					clsShared.cFile.EraseFile(text);
				}
				clsZip.Setup();
				clsZip.ZipFilename = text;
				clsZip.AddSingleFile(ProjectFilewithpath);
				if (clsZip.MakeZipFile() == 0)
				{
					clsShared.cLog.AddAction("Project file zipped to " + text);
				}
				else
				{
					clsShared.cLog.AddAction("Error zipping previous copy of project file to " + text);
				}
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
			}
		}

		public bool CopyCurrentProjectFile(clsProject cProject)
		{
			bool result = false;
			try
			{
				string text = string.Concat(str1: (!cProject.V6File) ? ".V5.bak" : ".V6.bak", str0: clsShared.cFile.FileWithoutExt(cProject.FileWithPath));
				if (Operators.CompareString(text, cProject.FileWithPath, TextCompare: false) != 0)
				{
					clsShared.cFile.EraseFile(text);
					if (!clsShared.cFile.FileExists(cProject.FileWithPath))
					{
						return result;
					}
					if (!clsShared.cFile.CopyFile(cProject.FileWithPath, text))
					{
						clsShared.cLog.AddAction("Unable to rename project to: " + text);
						return result;
					}
					clsShared.cLog.AddAction("Previous project file copied to: " + text);
					result = true;
					return result;
				}
				clsShared.cLog.AddAction("User has been editing their project BAK file instead of the YW5 file");
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public string LoadProjV6(string FileWithPath)
		{
			string TextString = "";
			clsShared.cFile.ReadUnicodeForced(FileWithPath, ref TextString);
			return TextString;
		}
	}
}
